package com.yjhb.android.sbz.mvp.balance;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.yjhb.android.sbz.entity.BalanceDetailed;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/4 15:37
 */
public class BalancePresenter extends ConventionPresenter<BalanceView> {
    private final String TAG = "BalancePresenter";

    public BalancePresenter(BalanceView balanceView) {
        super(balanceView);
    }

    @Override
    protected void onDestroy() {

    }


    public void getTaskBillHistory(int size, final int current) {
        HttpTool.request(model.getTaskBillHistory(size, current), new HttpObserver<JsonObject>() {
            @Override
            public void onResult(JsonObject data) {
                if (view != null) {
                    List<BalanceDetailed> balanceDetaileds = new GsonBuilder().
                            setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(data.get("records"),
                            new TypeToken<List<BalanceDetailed>>() {
                            }.getType());
                    if (current == 1)
                        view.onRefresh(balanceDetaileds);
                    else
                        view.onLoad(balanceDetaileds);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    public void earnToPay(){
        if (view!=null){
            view.onWait();
        }
        HttpTool.request(model.earnToPay(), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view!=null){
                    view.onSuccess();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view!=null){
                    view.onFailure(code, msg);
                }
            }
        });
    }

}
